<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir(dirname(__FILE__));

include_once $path."/../lib/core.class.php";



class StaticPage extends IPlugin 
{
    var $pagespool;
        var $c;
    function StaticPage($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->pagespool = null;
        $this->initialize();
    }
    
    function getPluginTag() { return "staticpage"; }
    function getPluginName() { return "Static page plugin"; }
    function getPluginVersion() { return "1.0.0"; }

    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuTitle", "MenuBlockTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Static page render plugin"; } break;
            case "MenuTitle" : { return "staticpage"; } break;
        };
        return $result;
    }
    
    function initialize()
    {
        $this->pagespool = array();
        if ($handle = opendir($this->tplfolder))
        {
            while (false !== ($file = readdir($handle)))
            {
                if("."==$file || ".."==$file || !file_exists($this->tplfolder."/".$file))
                {
                    continue;
                }
                $fl = split("\.", $file);
                $this->pagespool[] = $fl[0];
            }
            closedir($handle);
        }
        $this->pagespool = array_flip($this->pagespool);
        $core = &getCoreInstance();
        $core->setValues("staticpage_url", "?".$this->getURL()."&page=");
//      debug($this->pagespool);
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }

    
        switch ($_POST["event"]) {
                        
                        case "CR_App": {
                
                                //debug($_POST);
                                $msg = "
                <html>
                <body>
                    <b>Questions</b> <br /><br />
                    First Name - {$_POST[fname]} <br />
                    Last Name - {$_POST[lname]} <br />
                    E-mail - {$_POST[email]} <br />
                    Phone - {$_POST[phone]} <br />
                    Questions :<br /><br /> {$_POST[question]} <br />
                <body>
                </html>
                ";
                
                //echo $msg;
                //@mail($core->config['system']['info_email'], "Ask expert Questions", $msg);
                                      }
                                        break;
                        
            case "doContactUs": {
                $msg = "
                <html>
                <body>
                    <b>Questions</b> <br /><br />
                    First Name - {$_POST[fname]} <br />
                    Last Name - {$_POST[lname]} <br />
                    E-mail - {$_POST[email]} <br />
                    Phone - {$_POST[phone]} <br />
                    Questions :<br /><br /> {$_POST[question]} <br />
                <body>
                </html>
                ";
                
                //echo $msg;
               # @mail($core->config['system']['info_email'], "Ask expert Questions", $msg);
                                      }
                                        break;


            case "doRealSubmitSubscribe": {

                $core = &getCoreInstance();
                $db = &$core->getProperty("db");
                $render = &$core->framework->smarty;

//                if ($db->getOne("select count(*) from subscribe where email='" . str2db($_POST['_email']) . "'"))
//                {
//                    $render->assign("exist_subscribe_email", "Y");
//                }
//                else
//                {
//                    $render->assign("exist_subscribe_email", "N");

                                        $db->Exec("insert into subscribe (email, date_create, confirmation, fname, lname, country, isintrest, how_hear) values('" . str2db($_POST['_email']) . "', now(), 'N', '" . str2db($_POST['_fname']) ."','".str2db($_POST['_lname'])."','".str2db($_POST['_countrie'])."','".str2db($_POST['intrest'])."','".str2db($_POST['_knowus'])."')"); // "
                        $render->assign('status', 'Thank you for subscription.');
//                    $x =  $db->Exec("insert into subscribe (email, date_create, confirmation, fname, lname, country, isintrest, how_hear) values('" . str2db($_POST['_email']) . "', now(), 'N', '" . str2db($_POST['_fname']) ."','".str2db($_POST['_lname'])."','".str2db($_POST['_countrie'])."','".str2db($_POST['intrest'])."','".str2db($_POST['_knowus'])."')"); // "
//                    $id_subscribe = $x;
                      unset($_POST['event']);
//                    $header  = "MIME-Version: 1.0\r\n"; 
//                    $header .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
        
//                    $header .= "From: <{$core->config[system][info_email]}>\r\n"; 
    
                    //$header .= "Cc: {$core->config[system][info_email]}\r\n"; 
                    //$header .= "Bcc: {$core->config[system][info_email]}\r\n";
                    
//                    $str1 = strstr($_SERVER['HTTP_REFERER'], "?");
//                    $str2 = substr($_SERVER['HTTP_REFERER'], 0, strlen($_SERVER['HTTP_REFERER']) - strlen($str1));
                    
//                    $msg = "
//                    <html>
//                    <body>
//                        <b>Subscribe News</b> <br /><br />
//                        Hi url - {$str2}?path=staticpage&page=subscribe_news&id_subscribe={$id_subscribe}&email={$_POST[email_subscribe_news]} <br />
//                    <body>
//                    </html>";
                    
                    //echo $msg;
//                    @mail($_POST['email_subscribe_news'], "Subscribe News from The Invitation Coach", $msg, $header);
//                }
                

            }  break;   
      
            default:
                break;

        }
    }


    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }



        
        $core = &getCoreInstance();
        $render = &$core->framework->smarty;
        $db = &$core->getProperty("db");

        switch ($_G["page"]) {

            case "coming_soon" :
                    $page = "coming_soon.tpl.php";
                break;

            case "contact_us" :
                $render->assign("info_email", $core->config['system']['info_email']);
                $render->assign("sales_mail", $core->config['system']['sales_mail']);
                    $page = "contact_us.tpl.php";
                break;

            case "email_sent" :
                $this->rendersubpage2($core, $render, $db);
                $page = "email_send.tpl.php";
                break;

            case "rendersubpage2" :
                $this->rendersubpage2($core, $render, $db);
                $page = "submitsubscribe.tpl.php";
                break;

            case "rendersubpage3" :
                $this->renderSubscribeNewsFinish($core, $render, $db);
                $page = "rendersubpage3.tpl.php";
                break;

            case "subscribe_news" :
                $this->renderSubscribeNewsPage($core, $render, $db);
                $page = "subscribe_news.tpl.php";
                break;

            case "submitsubscribe" :
                $how_hear = $db->getAll("select how_id, how_title from how_hearus order by how_id");
                                $render->assign("how_hear", ArrayDb2Str($how_hear));
                $this->renderSubscribeNewsPage($core, $render, $db);
                $page = "submitsubscribe.tpl.php";
                break;


            case "contactus" :
                $this->renderContentContactusPage($core, $render, $db);
                $page = "content_contactus.tpl.php";
                break;

            case "faq" :
                $faqs = arraydb2str($db->getall("select * from faq where is_active > 0 order by date desc limit 20"));
                $render->assign("faqs", $faqs);

                $page = "faq.tpl.php";
                break;


            case "about" :
                //$this->renderAboutPage($core, $render, $db);
                $page = "about.tpl.php";
                break;
                                
                        case "public_relations" :
                $this->render_Public_relations_Page($core, $render, $db);
                $page = "public_relations.tpl.php";
                break;
                                
                        case "tradeshows" :
                $this->render_tradeshows($core, $render, $db);
                $page = "tradeshows.tpl.php";
                break;
                                
                        case "new_items" :
                //$this->renderAboutPage($core, $render, $db);

                                if ($this->getSessionCc() <= 0 ) 
                    {     
                        header("Location: ?path=ccontrl");
                    }
                        else
                    {
                        $this->RenderNewItemsPage($core, $render, $db);
                        $page = "new_items.tpl.php";
                        }
                    break;
                break;

                        case "credit" :
                //$this->renderAboutPage($core, $render, $db);
                $page = "credit.tpl.php";
                break;

                                

                        case "tutorial" :
                //$this->renderAboutPage($core, $render, $db);
                $page = "tutorial.tpl.php";
                break;
                        

            default:
                $this->renderIndexPage($core, $render, $db);
                $page = "index.tpl.php";
                break;
        }

        
        //debug($page);
        return $render->fetch("file:".$this->tplfolder."/".$page);
    }
    
// ----------------------------------------------------------------------------------
// --- [ Render Index pages ] -------------------------------------------------------------
// ----------------------------------------------------------------------------------
    
    function renderIndexPage(&$core, &$render, &$db)
    {
      $render->assign("catalog", $db->getall("select * from catalog where is_active='Y' and is_category='Y' limit 10"));
          $date_m=date("m")+0;
          if ( 3 <= $date_m && $date_m <= 5)  $date_period="Spring";
          if ( 6 <= $date_m && $date_m <= 8)  $date_period="Summer";
          if ( 9 <= $date_m && $date_m <= 11)  $date_period="Autumn";
          if ( 1 == $date_m || 2 == $date_m || 12 == $date_m )  $date_period="Winter";
          
                $render->assign("date_period", $date_period);
    }
    


// ----------------------------------------------------------------------------------
// --- [ Render Subscribe News pages ] ----------------------------------------------
// ----------------------------------------------------------------------------------
    
    function renderSubscribeNewsPage(&$core, &$render, &$db)
    {
        $db->Exec("delete from subscribe where date_create < DATE_SUB(CURDATE(),INTERVAL 3 DAY) and confirmation='N'");

        if (isset($_GET['id_subscribe']))
        {
            if ($db->getOne("select count(*) from subscribe where id=" . $_GET['id_subscribe'] . " and email='" . str2db($_GET['email']) . "'"))
            {
                $db->Exec("update subscribe set confirmation='Y' where id=" . $_GET['id_subscribe'] . " and email='" . str2db($_GET['email']) . "'");
                $render->assign("email_subscribe_news", "Thank you!!!");
            }
            else
            {
                $render->assign("email_subscribe_news", "No this e-mail");
            }
        }
        else
        {
            $email_subscribe_news = $_POST['email_subscribe_news'];
            $render->assign("email_subscribe_news", 
                "<b>Congratulation!</b><br />
                <br />
                Further information was successfully sent on you {$email_subscribe_news} e-mail<br />
                <br />
                Thanks you for your interest.
                ");
        }
        
    }

// ----------------------------------------------------------------------------------
// --- [ Render Content Contact us pages ] ----------------------------------------------
// ----------------------------------------------------------------------------------

    function renderContentContactusPage(&$core, &$render, &$db)    {
        if (isset($_POST['email'])) {
        $email = $_POST['email'];
        $render->assign("email", $email);
        }
        $data = ArrayDb2Str($db->getall("select title, catalog_id from catalog where parent_id ='0' order by catalog_id"));
        if ($data != '' and isne($data)) {
                
                $i = count($data);
                //debug($data);
                $tmp = '';
                for($x=0;$x<$i;$x++){
                        $tmp[$x] = ArrayDb2Str($db->getall("select title, catalog_id from catalog where parent_id ='".$data[$x]['catalog_id']."' and is_category not like 'Y'"));
                        
                        
                }
                
                for($x=0;$x<$i;$x++){
                        $data[$x]['items'] = $tmp[$x];
                        
                }
                
                
        
                /*      PHP 5.0                 
                foreach ($data as &$k){
                        $tmp = ArrayDb2Str($db->getall("select title, catalog_id from catalog where parent_id ='".$k['catalog_id']."' and is_category not like 'Y'"));
                        if (isne($tmp) and $tmp != ''){
                                $k['items'] = $tmp;
                        }
                }
                */
                
        
        }

    $render->assign("info_email", $core->config['system']['info_email']);
    $render->assign("sales_mail", $core->config['system']['sales_mail']);
        
        
        $render->assign("sitemap", $data);
        
    }

// ----------------------------------------------------------------------------------
// --- [ Render       FAQ      pages ] ----------------------------------------------
// ----------------------------------------------------------------------------------

    function RenderFAQpage(&$core, &$render, &$db)
    {
        $faqs = arraydb2str($db->getall("select * from faq where is_active > 0 order by date desc limit 20"));
        $render->assign("faqs", $faqs);


    }

// ----------------------------------------------------------------------------------
// --- [ Render Subscribe page 2 ] ----------------------------------------------
// ----------------------------------------------------------------------------------


    function rendersubpage2(&$core, &$render, &$db)
    {

                $core = &getCoreInstance();
                $db = &$core->getProperty("db");
                $render = &$core->framework->smarty;

                $email = $_POST['email_subscribe_news'];
                $ename = $_POST['name_subscribe_news'];
                
                $render->assign("xemail", $email);
                $render->assign("xename", $ename);
                
                $interestlist = $db->getAll("select catalog_id, title  from catalog where parent_id = 0 and is_category = 'Y' and display_in_menu = 'Y' order by sibling asc");
                $render->assign("top_menu", ArrayDb2Str($interestlist));
                
                $how_hear = $db->getAll("select how_id, how_title from how_hearus order by how_id");
                $render->assign("how_hear", ArrayDb2Str($how_hear));

    }

    function renderSubscribeNewsFinish(&$core, &$render, &$db)
    {

                $core = &getCoreInstance();
                $db = &$core->getProperty("db");
                $render = &$core->framework->smarty;
                
                $render->assign("email", ArrayDb2Str($_POST['_email']));
                        
    }

        function RenderNewItemsPage(&$core, &$render, &$db)
    {
                $param=0;
        $search_p="";
        if (isset($_POST[search_p])) $search=$_POST[search_p];
        if (isset($_GET[search_p])) $search=$_GET[search_p];
                $search_p=$search;
                if ($search!="" && $search!="(non selected)")
        {
            $post="&search_p=".$search;
            $range = explode("-",$search);
            $search_where .=  " and our_price between ".str2db($range[0])." and ".str2db($range[1]);
            //debug($range);
            if ($range[0]==1500) $result.="by price: <b> over $".$range[0]."</b>"; else 
            if ($range[1]==100) $result.="by price: <b> under $".$range[1]."</b>"; else
            $result.="by price: <b>$".$range[0]."-$".$range[1]."</b>";
            $param=1;
            $search=0;
        }
                $render->assign("search_p", $search_p);
                //
                
        //-----------------     paging[
        
        $p = $core->getSnippet("paging");
        $page = 0;
        $ppg = 0;
        $params = array(
            "source" => "sql",            
            "sql_count" => "select count(*) from catalog where is_active = 'Y' and is_category = 'N' $search_where",
            "ippg" => 6,
            "nppv" => 5,
                        "urlbase" => "?path=staticpage&page=new_items&search_p=$search_p",
            "page" => &$page,
            "ppg" => &$ppg,
            );
        $render->assign("paging", $p->Render($params));
        //-----------------     paging ]
        $list_products = ArrayDb2Str($db->getAll("select catalog_id,parent_id,item_number,title,is_active,img_small,our_price 
                                                                                                        from catalog 
                                                                                                        where is_active = 'Y' and is_category='N' $search_where order by date desc",
                                                                                0, false, $page==-1 ? 100000 : 6, $page==-1 ? 0 : $page*6));
                // ----------- check img + price format[
                for ($i=0;$i<count($list_products);$i++)
                {
                        if (file_exists($core->config["file_root"]."/inventory/".$list_products[$i][img_small]) && is_file($core->config["file_root"]."/inventory/".$list_products[$i][img_small])) {
                                list($w_size,$h_size)=explode(',',$this->getimgsizes($core->config["file_root"],$list_products[$i][img_small]));
                                $default_w_size=118;
                                $default_h_size=122;
                                $size="";
                                if ($w_size>$h_size && $w_size>$default_w_size)
                                        $size=" width=\"{$default_w_size}px\" ";
                                if ($h_size>$w_size && $h_size>$default_h_size)
                                $size=" height=\"{$default_h_size}px\" ";
                                
                                $list_products[$i][img_small] = "<img $size src=\"inventory/".$list_products[$i][img_small]."\" border=\"0\" >";
                        } else {
                                $list_products[$i][img_small] = "<img src='img/no_image.jpg' border=0 height1=80/>";
                        }
                        
                        $list_products[$i][our_price] = number_format($list_products[$i][our_price],2,'.',',');         
                }
                // ----------- check img + price format]
                
                $render->assign("list_products", $list_products);
        }
        
        
        function render_Public_relations_Page(&$core, &$render, &$db)
    {
                $public_relations = $db->getall("select * from public_relations where is_active='1'");
                
                //debug($public_relations);
                // ----------- check img + file format[
                for ($i=0;$i<count($public_relations);$i++)
                {
                        if (file_exists($core->config["file_root"]."/pdf/".$public_relations[$i][img]) && is_file($core->config["file_root"]."/pdf/".$public_relations[$i][img])) {
                                list($w_size,$h_size)=getimagesize($core->config["file_root"]."/pdf/".$public_relations[$i][img]);
                                //debug($core->config["file_root"]);debug($public_relations[$i][img]);
                                //debug($w_size);debug($h_size);
                                $default_w_size=188;
                                $default_h_size=188;
                                $size="";
                                if ($w_size>$h_size && $w_size>$default_w_size)
                                        $size=" width=\"{$default_w_size}px\" ";
                                if ($h_size>$w_size && $h_size>$default_h_size)
                                $size=" height=\"{$default_h_size}px\" ";
                                
                                $public_relations[$i][img] = "<img $size src=\"pdf/".$public_relations[$i][img]."\" border=\"0\" >";
                        } else {
                                $public_relations[$i][img] = "<img src='pdf/no_image.jpg' border=0 height1=80/>";
                        }
                }
                //debug($public_relations);
                // ----------- check img + price format]
                $render->assign("public_relations", $public_relations   );
                        
    }
        
        function render_tradeshows(&$core, &$render, &$db)
    {
                $tradeshows = arraydb2str($db->getall("select * from tradeshows where is_active='1' order by date asc"));
                
                //debug($tradeshows);
                
                $render->assign("tradeshows", $tradeshows       );
                        
    }
        
        function getSessionCc()                                 {
                        $x = $_SESSION['cc']['client_id'];
                        return($x);
                                
                }
        // ----------------------------------------------------------------------------------
        // --- [ GET IMAGES SIZE ] ----------------------------------------------------------
        // ----------------------------------------------------------------------------------

        function getimgsizes($froot,$img){ 
                if(isne($img) && is_file($froot.'/inventory/'.$img)) {
                        $img = $froot.'/inventory/'.$img;
                        $ar = getimagesize($img);
                        $arrr = $ar[0].','.($ar[1]+50);
                        return $arrr;
                }
                return "";
        } 

};


?>
